package adb;

import static adb.ADBView.WriteFileTMP;
import static adb.ADBView.adbVariable;
import static adb.ADBView.getADBfolderAndsetFullADBpath;
import static adb.ADBView.jProgressBarDoInBackground;
import static adb.ADBView.jTextAreaStatus;
import static adb.ADBView.listApps;
import static adb.ADBView.output;
import static adb.ADBView.readSavedADB;
import static adb.ADBView.removeFileinPath;
import static adb.ADBView.separator;
import static adb.ADBView.showPCTree;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.swing.JFileChooser;
import javax.swing.ListSelectionModel;
import javax.swing.SwingWorker;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class LoadingWindows extends SwingWorker<Void, String> {

    @Override
    protected Void doInBackground() throws Exception {

        int progress = 0;
        jProgressBarDoInBackground.setValue(progress);
        getADBpath();
        //System.out.print(adb+" <<< adb after getADBpath\n");
        listApps.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        showPCTree();
        
        progress = 30;
        jProgressBarDoInBackground.setValue(progress);
        String command = adbVariable + " devices";
        if (command.charAt(0) != '0') {
            String[] cc = command.split(" ");
            try {
                String line = "";
                ProcessBuilder pb = new ProcessBuilder(cc);
                pb.redirectErrorStream(true);
                Process process = pb.start();
                BufferedReader inStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                progress = 50;
                jProgressBarDoInBackground.setValue(progress);
                while ((line = inStreamReader.readLine()) != null && !"".equals(line)) {
                    if (line.length() > 0) {
                        //out = out + line + "\n";
                        output = output + line + "\n";
                        jTextAreaStatus.setText(output);
                        //System.out.print(line+"\n");
                        progress = progress + 10;
                        jProgressBarDoInBackground.setValue(progress);
                    }
                }
            } catch (IOException ex) {
                output = output + ex + "----ex2\n";
                jTextAreaStatus.setText(output);
            }
        }
        //jTextAreaStatus.setText(output);
        jProgressBarDoInBackground.setValue(0);
        return null;
    }

    
    static void getADBpath() {
        // get current jAR PATH
        String ADBJarPath = getJarpath();
        if (ADBJarPath.contains(".jar")) {
            ADBJarPath = removeFileinPath(ADBJarPath);
        //jTextAreaStatus.setText(output=output+ADBJarPath+"ADBJarPath\n");
        }
        // CHECK IF ADB PLACE IN CURRENT JAR PATH
        File adbfullpath = null;
        adbfullpath = new File(ADBJarPath + "adb.exe");
        //jTextAreaStatus.setText(output=output+adbfullpath+"adbfullpath\n");
        if (adbfullpath.isFile()) {// if adb same as current Jar file set to abd AND finish
        getADBfolderAndsetFullADBpath(ADBJarPath);
        //jTextAreaStatus.setText(output=output+adb+"adb getADBfolderAndsetFullADBpath\n");
        } else {// IF ADB NOT IN CURRENT PATH, CHECK IN DATA .ADB
            String ADBpathFromData = readSavedADB();
            if (ADBpathFromData.length() > 1) {
                File adbfullpath2 = null;
            adbfullpath2 = new File(ADBpathFromData + "adb.exe");
                if (adbfullpath2.isFile()) {
                    getADBfolderAndsetFullADBpath(ADBpathFromData);
                }else openFileChooserAndWrite();
        
            } else {
                openFileChooserAndWrite();
            }
        }
    }
    static void openFileChooserAndWrite() {
        jTextAreaStatus.setText("Select adb.exe ...\n");
        JFileChooser openAdb = new JFileChooser();
        FileFilter filter = new FileNameExtensionFilter("ADB.exe File", "exe");
        openAdb.setFileFilter(filter);
        openAdb.setMultiSelectionEnabled(false);
        openAdb.setName("Select ADB file");
        openAdb.setDialogTitle("Select adb.exe file");
        int state = openAdb.showOpenDialog(ADBView.jLabelToLeft);
        if (state == 1) {
            jTextAreaStatus.setText(output = output + "Select adb.exe first.\nExit and try again.\n");
        }
        adbVariable = openAdb.getSelectedFile().toString();
        
            int i = adbVariable.length() - 1;
            while (adbVariable.charAt(i) != '\\') {
                adbVariable = adbVariable.substring(0, i);
                i--;
            }
        WriteFileTMP(adbVariable, 1);
        getADBfolderAndsetFullADBpath(adbVariable);
        //jTextAreaStatus.setText(output = output + adb+"<<<adb. end of filechooser\n");
    }
    public static String getJarpath(){
    String ADBJarPath = ADBView.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        try {
            ADBJarPath = URLDecoder.decode(ADBJarPath, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            output = output + ex + "----ex17\n";
            jTextAreaStatus.setText(output);
        }
        if (ADBJarPath.charAt(0) == '/') {
            ADBJarPath = ADBJarPath.substring(1);
        }
        {
            ADBJarPath = ADBJarPath.replace("/", separator);
        }
        return ADBJarPath; 
    }
    @Override
    protected void done() {
        jProgressBarDoInBackground.setValue(0);
    }
}
